// Copyright (C) 2012 by Johann Duscher <jonny.dee@gmx.net>
//
// This file is part of barcodeart-pp.
//
// barcodeart-pp is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// barcodeart-pp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with barcodeart-pp. If not, see <http://www.gnu.org/licenses/>.

#include <cassert>

#include <barcodeart/IBarCode.hpp>

#include "barcodeart/svgwriter/SVGWriter.hpp"
#include "barcodeart/svgwriter/SVGWriterConfig.hpp"


namespace barcodeart
{

namespace svgwriter
{

int SVGWriter::write(std::ostream& out, const IBarCode& barCode, const IWriterConfig& iConfig) const
{
    const SVGWriterConfig& config = dynamic_cast<const SVGWriterConfig&>(iConfig);

    /* SVG XML file header */
    out << "<?xml version=\"1.0\" standalone=\"no\"?>\n"
           "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";

    /* start */
    out << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n";

    /* data */
    for(int y = 0; y < barCode.getHeight(); y++)
    {
        for(int x = 0; x < barCode.getWidth(); x++)
        {
            if(barCode.isBlack(y, x))
                out << "  <rect x=\"" << x*config.getPixelWidth() << "\" y=\"" << y*config.getPixelHeight()
                    << "\" width=\"" << config.getPixelWidth() << "\" height=\"" << config.getPixelHeight()
                    << "\" style=\"fill:black\" />\n";
        }
    }

    /* end */
    out << "</svg>\n";

    return 0;
}

}

}
